<template>
  <div>
    <v-tableTitle
      :btnConfig="btnConfig"
      :tableSelectData="tableSelectData"
      :title="$t('home1.dispatchList')"
      @newCreate="newCreate"
      @del="del"
      @handleDownErrorFile="exceptionFileUrl"
      @handleDownTemplate="downloadExcelFile"
    ></v-tableTitle>
    <GTable
      ref="table"
      api="dcDeliveryOrder/selectDcDeliveryOrderList"
      :hasCheck="true"
      :hasEdit="true"
      :otherHeight="0"
      :columnConfig="columnConfig"
      @selection-change="selectData"
      @setdialog="setdialog"
    >
    </GTable>
  </div>
</template>

<script>
import tableTitle from "commons/TableTitle";
import baseMixin from "@/mixins/BaseMixin";

export default {
  name: "asn",
  mixins: [baseMixin],
  data () {
    return {
      columnConfig:{ menu_id: 1012, menu_code:"dispatchList", menu_list_code:"dispatchList", },
      tableSelectData: []
    };
  },
  computed: {
    btnConfig () {
      return [
        {label: this.$t('common.create'), certigier: !this.btnPermNameList.includes('WM010104'), invalid: "newCreate"},
        {type: "file", certigier:!this.btnPermNameList.includes('WM010101'), label: this.$t('common.import'), invalid: "file",importType:'10321',importUrl:"dcDeliveryOrder/importDeliveryOrder",downTemplate:"handleDownTemplate",sheetKey:"dcDeliveryOrderDtlList"},
        {label: this.$t('common.delete'), certigier: !this.btnPermNameList.includes('WM010103'), disabled: true, invalid: "del"},
      ];
    }
  },
  created () {
  },
  methods: {
    exceptionFileUrl(exceptionUrl) {

      console.log("exceptionFileUrl = %o", exceptionUrl);
      const data = [{
        exceptionFileUrl: exceptionUrl
      }];

      // 异常下载配置
      let urlConf = {responseType: 'blob'};
      let exceptionFileName = "配送单导入模版异常文件.xls";
      let url = '/comExcelImportResult/downloadExceptionFile';
      let options = {type: 'application/vnd.openxmlformats-officedocument.wordprocessingml.document;charset=utf-8'};

      try {
        this.$api.post(url, data, urlConf).then(res => {
          let blobParts = [res.data];
          if (window.navigator.msSaveBlob) {
            const blobObject = new Blob(blobParts);
            window.navigator.msSaveBlob(blobObject, exceptionFileName);
          } else {
            let tagName = 'a';
            const blob = new Blob(blobParts, options);
            const downloadElement = document.createElement(tagName);
            // 创建下载的链接
            const href = window.URL.createObjectURL(blob);
            downloadElement.style.display = 'none';
            downloadElement.href = href;
            // 下载后文件名
            downloadElement.download = exceptionFileName;
            document.body.appendChild(downloadElement);
            // 点击下载
            downloadElement.click();
            // 下载完成移除元素
            document.body.removeChild(downloadElement);
            // 释放掉blob对象
            window.URL.revokeObjectURL(href);
          }
        });
      } catch (e) {
        this.$message.error(e);
      }
    },
    //新建
    newCreate() {
      this.$router.push({name:"dispatchDetail"});
    },
    async downloadExcelFile(data) {
      try {
        let xlsName = "配送单导入模板.xls";
        let url = "/downloadExcelFile/" + xlsName;
        let res = await this.$api.post(url, {}, {responseType: 'blob'})
        if (window.navigator.msSaveBlob) {
          var blobObject = new Blob([res.data]);
          window.navigator.msSaveBlob(blobObject, xlsName);
        } else {
          var blob = new Blob([res.data], {type: 'application/vnd.openxmlformats-officedocument.wordprocessingml.document;charset=utf-8'});
          var downloadElement = document.createElement('a');
          var href = window.URL.createObjectURL(blob); //创建下载的链接
          downloadElement.style.display = 'none';
          downloadElement.href = href;
          downloadElement.download = xlsName; //下载后文件名
          document.body.appendChild(downloadElement);
          downloadElement.click(); //点击下载
          document.body.removeChild(downloadElement); //下载完成移除元素
          window.URL.revokeObjectURL(href); //释放掉blob对象
        }
      } catch (e) {
        this.$message.error(e);
      }
    },
    //选中的内容
    selectData (data) {
      this.tableSelectData = data;
    },
    //删除
    async del(data) {
      try {
        const ids = data.map(item => item.id).join(",")
        let res = await this.$post(`/dcDeliveryOrder/deleteDcDeliveryOrderByIds/${ids}`);
          this.$message.success(res.returnMgr);
          this.$refs.table.getTableData();
        
      } catch (e) {
        this.$message.error(e);
      }
    },
    //设置弹框
    setdialog (data) {
      this.$router.push({name:"dispatchDetail",query:{id:data.id}})
    }

  },
  mounted () {
  },
  components: {
    "v-tableTitle": tableTitle,
  }
};
</script>

<style lang="scss" scoped>

</style>
